/*
* Copyright (c) 2022，公司名称
* All rights reserved.
* 文件名称：high_side.c
* 摘要：低边MCU处理
* 当前版本：V1.0.0,WCD,2023/1/10,初版
* 历史版本：Vx.x.x，编写者/修改者，修改时间，修改内容（重大发布时，总结重点变更内容）
*/
#include "high_includes.h"
#define TEMP_DEBUG
#define HV_R2 (5.1)
#define HV_R_SUM (605.1)
/*ADC转换为实际电压*/
#define HV_ADC_CONVERT(x) (HV_R_SUM / HV_R2 * ADC_TO_VOLTAGE(x))
#define CURR_BUFF_LEN (10u)

#define POWER_MAX (5000u)
//#define POWER_MAX (40u)

#define POWER_PID_TIME (10u)
#define TEMP_PID_TIME (1000u)

//#define ACC_TIME (2000u)
#define ACC_TIME (50)
#define START2_TIME (8000u)

#define TIME_0_5S (500u)
#define TIME_1S (1000u)
#define TIME_5S (5000u)

//#define CURRENT_0_5A (0.5)
#define CURRENT_0_5A (1)
#define CURRENT_3A (180)
#define CURRENT_30A (30)
/*比较器AMP端口*/
#define CURRENT_60A (60)

#define HV_OVER_VALUE (470u)
#define HV_OVER_CANCEL_VALUE (450u)

#define HV_INVALID_VALUE (2u)
#define HV_INLAY_VALUE (220u)
#define HV_INLAY_CANCEL_VALUE (250u)

#define VBAT_3V_VALUE (614u)
#define VBAT_3_5V_VALUE (716u)

/*start 10%*/
#define PWM_ACC_START (0u)
/*10%*/
//#define PWM_ACC_END (410u)
/*20%*/
//#define PWM_ACC_END (820)
/*50%*/
//#define PWM_ACC_END (2050u)
/*60*/
//#define PWM_ACC_END (2457)
/*70*/
//#define PWM_ACC_END (2866)
/*80%*/
//#define PWM_ACC_END (3280u)
/*90%*/
//#define PWM_ACC_END (3685)
/*100%*/
#define PWM_ACC_END (4095)
/*调试时禁止关闭*/
//#define DISABLE_OFF_DEBUG
typedef struct
{
    uint16_t adc_voltage;
    uint16_t hv_voltage;
    uint16_t v15;
    uint16_t pcb_temp;
} last_adc_struct;
typedef enum
{
    PWM_OFF_NOT = 0u,
    PWM_OFF_TEST_PWM,
    PWM_OFF_IGBT_OPEN,
    PWM_OFF_IGBT_SHORT,
    PWM_OFF_CUR_OVER30,
    PWM_OFF_CUR_OVER60,
    PWM_OFF_POWER0,
    PWM_OFF_MODE_ERR,
    PWM_OFF_CONDITION,
    PWM_OFF_ACTIVE,
} PWM_OFF_type;
PWM_OFF_type pwm_off_st = PWM_OFF_ACTIVE;
high_side_status high_side_st;
static uint16_t cur_buff[CURR_BUFF_LEN] = {0};
static uint16_t add_pwm_time1 = 0;
static uint8_t add_pwm_time2 = 0;
static uint32_t Tick_cnt_pwm = 0u;

uint8_t igbt_open_test = 0;

/*
 * 函数介绍: ADC数值转换
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void high_ADC_convert(void)
{
    static uint8_t curr_buff_count = 0u;
    static uint16_t curr_min = 0xfffu;
    static uint16_t curr_max = 0u;
    static last_adc_struct last_adc_st = {0u};
    static uint32_t curr_total = 0u;
    static uint32_t set_Tick_cnt = 0u;
    static float last_pwm_duty = 0;
    float pwm_duty = pwm_get_duty();

    __IO uint32_t reg_data = get_current_ADC_value();
    __IO uint16_t adc_data = reg_data & 0xffff;

    if (last_adc_st.hv_voltage != high_ADC_OrgiData.hv_voltage.avg ||
        last_pwm_duty != pwm_duty)
    {
        last_pwm_duty = pwm_duty;
        last_adc_st.hv_voltage = high_ADC_OrgiData.hv_voltage.avg;
        high_side_st.DC_HV = HV_ADC_CONVERT(high_ADC_OrgiData.hv_voltage.avg);
//        high_side_st.HV = high_side_st.DC_HV * pwm_duty + 0.5;
        high_side_st.HV = high_side_st.DC_HV;
    }
    else
    { ;
    }

    /*0: This register is read, or A/D conversion is not finished.
    1: The A/D conversion result is stored (not read yet). */
    if (reg_data & (uint32_t) (1 << 25))
    {
#if 1
        if (PTC_MODE_PRE_INIT == high_side_st.ptc_mode_st)
        {
            high_side_st.pre_check_current = CURR_CONVERT(adc_data);
            pwm_off();
            return;
        }
        else
        { ;
        }
#endif
        set_Tick_cnt = Get1MsTickVal();
//        adc_data = high_ADC_OrgiData.pcb_temp.avg;
//        R_PORT_SetGpioOutput(Port0, 0, Low);
        curr_buff_count++;
        if (curr_buff_count < CURR_BUFF_LEN)
        { ;
        }
        else
        {
            curr_buff_count = 0u;
        }
        cur_buff[curr_buff_count] = adc_data;
        curr_total += adc_data;

        if (adc_data < curr_min)
        {
            curr_min = adc_data;
        }
        else if (adc_data > curr_max)
        {
            curr_max = adc_data;
        }
        else
        { ;
        }

        if ((CURR_BUFF_LEN - 1) == curr_buff_count)
        {
            uint16_t adc_avg = (curr_total - curr_min - curr_max) / (CURR_BUFF_LEN - 2);
            __IO float adc_voltage = ADC_TO_VOLTAGE(adc_avg - high_side_st.current_init);
//            __IO float adc_voltage = ADC_TO_VOLTAGE(adc_avg);
            high_side_st.current_act = CURR_CONVERT(adc_voltage);
            high_side_st.current = CURR_CONVERT(adc_voltage) * pwm_duty;
            high_side_st.act_power = high_side_st.current * high_side_st.HV;

            curr_min = 0xfffu;
            curr_max = 0u;
            curr_total = 0u;
//            R_PORT_SetGpioOutput(Port0, 0, High);
        }
        else
        { ;
        }
    }
    else
    { ;
    }

    if (1 == Get1MsTickInterval(set_Tick_cnt,20))
    {
        high_side_st.current = 0u;
    }
    else
    { ;
    }

    if (last_adc_st.v15 != high_ADC_OrgiData.V15.avg)
    {
        last_adc_st.v15 = high_ADC_OrgiData.V15.avg;
        high_side_st.V15_voltage = VBAT_CONVERT(ADC_TO_VOLTAGE(high_ADC_OrgiData.V15.avg));
    }
    else
    { ;
    }
    if (last_adc_st.pcb_temp != high_ADC_OrgiData.pcb_temp.avg)
    {
        last_adc_st.pcb_temp = high_ADC_OrgiData.pcb_temp.avg;
        high_side_st.pcb_temp = ntc_get_temp(NTC_PCB, high_ADC_OrgiData.pcb_temp.avg) + 0.5;
    }
    else
    { ;
    }
}
void high_out_temp(void)
{
    static uint32_t tick = 0u;

    if (tick == 0u)
    {
        tick = Get1MsTickVal();
    }
    else
    { ;
    }

    if (1 == Get1MsTickInterval(tick,10))
    {
        tick = Get1MsTickVal();
        high_side_st.act_temp = high_uart30_get_out_temp();
    }
    else
    { ;
    }
}
void high_ptc_mode(void)
{
    if (HVH_MODE_OFF != high_uart_rx_st.sig.ptc_off_on)
    {
        /*高边模式关闭*/
        if (PTC_MODE_OFF == high_side_st.ptc_mode_st)
        {
//            if (!high_side_st.cur_over60_flag && VOLTAGE_VALID == high_side_st.HV_voltage_st && !high_side_st.cur_over30_flag)
                if (PTC_ERROR_NONE == high_uart_tx_st.sig.ptc_all_error_status ||
                    PTC_ERROR_PCB_OVER_120 == high_uart_tx_st.sig.ptc_all_error_status)
            {
                high_side_st.ptc_mode_st = PTC_MODE_INIT;

            }
            else
            {
                NOP;
            }
        }
        else
        {;
        }
    }
    else
    {
//        high_side_st.ptc_mode_st = PTC_MODE_OFF;
    }
}
/*
 * 函数介绍: PID调试功率
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void high_send_data_handle(void)
{
    high_uart_tx_st.sig.dc_curr = (uint8_t) (high_side_st.current / 4);
    high_uart_tx_st.sig.hv_voltage = high_side_st.HV / 2;
    high_uart_tx_st.sig.ptc_status = high_side_st.ptc_status;
    high_uart_tx_st.sig.pcb_temp = high_side_st.pcb_temp;
    high_uart_tx_st.sig.HV_voltage_st = high_side_st.HV_voltage_st;
    high_uart_tx_st.sig.ntc_status = high_side_st.ntc_status;
    high_uart_tx_st.sig.IGBT_status = high_side_st.IGBT_status;
    high_uart_tx_st.sig.cur_over30 = high_side_st.cur_over30_flag;
    high_uart_tx_st.sig.power = high_side_st.act_power / 20;
}
/*
 * 函数介绍: IGBT 检测函数
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void high_IGBT_init_check(void)
{
    static uint32_t tick = 0u;
    if (FALSE == high_side_st.IGBT_check)
    {
        if (tick == 0)
        {
            tick = Get1MsTickVal();
            PWM_IGBT_duty(200);
            pwm_on();
        }
        else
        { ;
        }

        if ( 1 == Get1MsTickInterval(tick,5))
        {
            if (high_side_st.pre_check_current < 50)
            {
                high_side_st.IGBT_init_status = IGBT_ST_OPEN;
            }
            else if (high_side_st.current_init >= 50)
            {
                high_side_st.IGBT_init_status = IGBT_ST_SHORT;
            }
            else
            {
                high_side_st.ptc_mode_st = PTC_MODE_INIT;
                high_side_st.IGBT_init_status = IGBT_ST_VALID;
            }
        }
        else
        { ;
        }
    }
    else
    { ;
    }
}
/*
 * 函数介绍: 预启动
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void ptc_start1_handle(void)
{
    if (high_side_st.cur_pwm1 < PWM_ACC_END)
    {
        if ( 1 == Get1MsTickInterval(Tick_cnt_pwm,50))
        {
            Tick_cnt_pwm = Get1MsTickVal(); /* 获取计数值 */
            high_side_st.cur_pwm1 += add_pwm_time1;
            if (high_side_st.cur_pwm1 > PWM_ACC_END)
            {
                high_side_st.cur_pwm1 = PWM_ACC_END;
            }
            else
            { ;
            }
            /*PWM_IGBT1_duty(high_side_st.cur_pwm1)*/;
            PWM_IGBT_duty(high_side_st.cur_pwm1);
        }
        else
        { ;
        }
    }
    else
    {
        high_side_st.ptc_mode_st = PTC_MODE_RUN;
    }
}
/*
 * 函数介绍: 预启动2
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void ptc_start2_handle(void)
{
    if (high_side_st.cur_pwm2 < PWM_ACC_END)
    {
        if (1 == Get1MsTickInterval(Tick_cnt_pwm,add_pwm_time2))
        {
            Tick_cnt_pwm = Get1MsTickVal(); /* 获取计数值 */
            high_side_st.cur_pwm2++;
            /*PWM_IGBT2_duty(high_side_st.cur_pwm2);*/
            PWM_IGBT_duty(PID_power_st.Out);
        }
        else
        { ;
        }
    }
    else
    {
        high_side_st.ptc_mode_st = PTC_MODE_RUN;
    }
}
/*
 * 函数介绍: 指令转换功率
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void high_ptc_power(void)
{
    if (high_uart_rx_st.sig.pwm_percent < 100)
    {
        float temp = high_uart_rx_st.sig.pwm_percent / 100.0;
        high_side_st.set_power = temp * POWER_MAX;
    }
    else
    {
        high_side_st.set_power = POWER_MAX;
    }
#if 0
    if (high_uart_rx_st.sig.pwm_percent == 100U)
    {
        high_side_st.set_power = 5000u;
    }
    else if (high_uart_rx_st.sig.pwm_percent > 83U)
    {
        high_side_st.set_power = 4150u;
    }
    else if (high_uart_rx_st.sig.pwm_percent > 67U)
    {
        high_side_st.set_power = 3350u;
    }
    else if (high_uart_rx_st.sig.pwm_percent > 50U)
    {
        high_side_st.set_power = 2500u;
    }
    else if (high_uart_rx_st.sig.pwm_percent > 33U)
    {
        high_side_st.set_power = 1650u;
    }
    else if (high_uart_rx_st.sig.pwm_percent > 17U)
    {
        high_side_st.set_power = 850u;
    }
    else
    {
        high_side_st.set_power = 0u;
    }
#endif
}
/*
 * 函数介绍: PID功率调节
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void high_pid_power_handle(uint16_t set_power)
{
    if (set_power != 0u)
    {

        PID_power_st.Ref = set_power;
        PID_power_st.Fdb = high_side_st.act_power;
//        pid_func(&PID_power_st);
        pid_func_new(&PID_power_st);
#if 0
        PWM_IGBT1_duty(PID_power_st.Out);
        PWM_IGBT2_duty(PID_power_st.Out);
#endif
        PWM_IGBT_duty(PID_power_st.Out);
        high_side_st.set_pwm = PID_power_st.Out;
    }
    else
    {
        pwm_off_st = PWM_OFF_POWER0;
    }
}

/*
 * 函数介绍: PID温度调节
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void high_pid_temp_handle(uint8_t set_temp, float act_temp)
{
//    PID_temp_st.Ref = set_temp;
    PID_temp_st.Fdb = act_temp;
    pid_func(&PID_temp_st);
#if 0
    PWM_IGBT1_duty(PID_temp_st.Out);
    PWM_IGBT2_duty(PID_temp_st.Out);
#else
    PWM_IGBT_duty(PID_temp_st.Out);
//    high_side_st.set_pwm = PID_temp_st.Out;
#endif
}

/*
* 函数介绍 PID模式选择
* 参数：
* 返回值：
* 备注：
*/
void high_PID(void)
{
    static uint32_t power_tick = 0u;
    static uint32_t temp_tick = 0u;

    if (power_tick == 0)
    {
        power_tick = Get1MsTickVal();
    }
    else
    { ;
    }

    if (temp_tick == 0)
    {
        temp_tick = Get1MsTickVal();
    }
    else
    { ;
    }
#ifndef  TEMP_DEBUG
    if ( 1 == Get1MsTickInterval(power_tick,POWER_PID_TIME) )
    {
        power_tick = Get1MsTickVal();

        if (HVH_MODE_POWER  == high_uart_rx_st.sig.ptc_off_on)
        {
            static uint8_t last_percent = 0u;
            if (PWM_OFF_MODE_ERR == pwm_off_st)
            {
                pwm_off_st = PWM_OFF_NOT;
            }
            else
            { ;
            }
            if (last_percent != high_uart_rx_st.sig.pwm_percent)
            {
                high_ptc_power();
                last_percent = high_uart_rx_st.sig.pwm_percent;
            }
            else
            { ;
            }
//            high_pid_power_handle(high_side_st.set_power);
            high_pid_power_handle(PID_power_st.Ref);
        }
        else
        { ;
        }
    }
    else
    { ;
    }
#endif

    if ( 1 == Get1MsTickInterval(temp_tick,TEMP_PID_TIME))
    {

        temp_tick = Get1MsTickVal();

#ifndef TEMP_DEBUG
        if (PTC_MODE_TEMP == high_uart_rx_st.sig.ptc_off_on)
#endif
        {
            if (PWM_OFF_MODE_ERR == pwm_off_st)
            {
                pwm_off_st = PWM_OFF_NOT;
            }
            else
            { ;
            }
            high_pid_temp_handle(high_uart_rx_st.sig.set_temp, high_side_st.act_temp);
        }
#ifndef TEMP_DEBUG
        else
        { ;
        }
#endif
    }
    else
    {

    }
}

void high_mo_test(void)
{
    static uint32_t run_tick = 0u;
    static uint8_t run = 1u;

    if (run_tick == 0)
    {
        run_tick = Get1MsTickVal();
    }

    if ((1 == run) &&  1 == Get1MsTickInterval(run_tick,20000) )
    {
        run = 0;
        run_tick = Get1MsTickVal();

//        pwm_off();
    }

    if ((0 == run) && 1 == Get1MsTickInterval(run_tick,2000))
    {
        run = 1;
        run_tick = Get1MsTickVal();
        PWM_IGBT_duty(PWM_ACC_END);

        pwm_on();
    }
}

/*
 * 函数介绍: IGBT状态机
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void high_IGBT_status(void)
{
    static uint32_t run_tick = 0u;
    static uint32_t init_tick = 0u;

    switch (high_side_st.ptc_mode_st)
    {
        case PTC_MODE_PRE_INIT:
#if 0
            high_IGBT_init_check();
#else
            high_side_st.ptc_mode_st = PTC_MODE_INIT;
#endif
            init_tick = Get1MsTickVal();
            break;
        case PTC_MODE_INIT:
            if (init_tick == 0u)
            {
                init_tick = Get1MsTickVal();
            }
            else
            { ;
            }

            if ( 1 == Get1MsTickInterval(init_tick,10))
            {
#ifndef DISABLE_OFF_DEBUG
                if (high_side_st.HV > 220)
#else
                    if (1)
#endif
                {
                    init_tick = 0u;
                    SHORT_PIN_INPUT();
                    pid_init(&PID_power_st);
                    high_uart_tx_st.sig.cur_short = FALSE;
                    high_side_st.cur_pwm1 = 0u;
                    high_side_st.cur_pwm2 = 0u;
                    add_pwm_time1 = (PWM_ACC_END - PWM_ACC_START) / (ACC_TIME / 50);
                    high_side_st.cur_pwm1 = PWM_ACC_START;
                    /*add_pwm_time2 = add_pwm_time1;*/
                    Tick_cnt_pwm = Get1MsTickVal();
                    high_side_st.ptc_mode_st = PTC_MODE_START1;
                    PWM_IGBT_duty(PWM_ACC_START);
                    pwm_on();
                }
                else
                {
                    init_tick = Get1MsTickVal();
                }

            }
            else
            { ;
            }
            break;

        case PTC_MODE_START1:
            ptc_start1_handle();
            /*test code*/
            run_tick = 0;
            break;
#if 0
            case PTC_MODE_START2:
                ptc_start2_handle();
                break;
#endif
        case PTC_MODE_RUN:

            if (run_tick == 0)
            {
                run_tick = Get1MsTickVal();
            }

            if ( 1 == Get1MsTickInterval(run_tick,5000))
            {
                PWM_IGBT_duty(4095);
//                pwm_off();
            }
#if 1
//            high_PID();
#ifndef PWM_TEST
//            high_PID();
#else

#endif
#else
            high_mo_test();
#endif
            break;

        case PTC_MODE_SHUTDOWN:
            high_side_st.cur_pwm1 = 0u;
            high_side_st.cur_pwm2 = 0u;
            break;
        case PTC_MODE_OFF:
            pwm_off_st = PWM_OFF_ACTIVE;
            pwm_off();

            init_tick = Get1MsTickVal();
            break;
        default:
            break;
    }
}
/*
 * 函数介绍: 短路检测
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void high_cur60A_hand(void)
{
    static uint32_t Tick_cnt_5s = 0u;
    volatile uint32_t pin_short = SHORT_PIN_LEVEL;
    uint16_t current = high_side_st.current;

//    if ((FALSE == high_side_st.cur_over60_flag) && (current >= 60 || pin_short) &&
//        high_side_st.ptc_mode_st != PTC_MODE_INIT)
        if ((FALSE == high_side_st.cur_over60_flag) && (current >= 60 || pin_short))
    {
        high_side_st.cur_over60_count++;
        high_side_st.cur_over60_flag = TRUE;
        high_side_st.ptc_mode_st = PTC_MODE_OFF;
        pwm_off_st = PWM_OFF_CUR_OVER60;
        Tick_cnt_5s = Get1MsTickVal();
        high_uart_tx_st.sig.cur_short = TRUE;
    }
    else
    {
    }

    if (high_side_st.cur_over60_flag == TRUE &&  1 == Get1MsTickInterval(Tick_cnt_5s,TIME_5S))
    {
//        if (PTC_MODE_OFF == high_side_st.ptc_mode_st && high_side_st.cur_over60_count <= 3u)
            if (high_side_st.cur_over60_count < 3u)
        {
            high_side_st.cur_over60_flag = FALSE;
            high_side_st.ptc_mode_st = PTC_MODE_INIT;
            SHORT_PIN_Low();
        }
        else
        {
            NOP;
        }
    }
    else
    { ;
    }
}
/*
 * 函数介绍: 过流检测
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void high_cur30A_hand(void)
{
    static uint32_t set_Tick_cnt = 0u;
    static uint32_t Tick_cnt_5s = 0u;
    uint8_t current = high_side_st.current;

//    current =40;

//    if ((FALSE == high_side_st.cur_over30_flag) && current >= CURRENT_30A &&
//        current < CURRENT_60A && PTC_MODE_OFF != high_side_st.ptc_mode_st)
        if ((FALSE == high_side_st.cur_over30_flag) && current >= CURRENT_30A &&
            current < CURRENT_60A )
    {
        if ( 1 == Get1MsTickInterval(set_Tick_cnt,TIME_1S))
        {
            high_side_st.cur_over30_count++;
            high_side_st.cur_over30_flag = TRUE;
            high_side_st.ptc_mode_st = PTC_MODE_OFF;

            pwm_off_st = PWM_OFF_CUR_OVER30;
            Tick_cnt_5s = Get1MsTickVal();
        }
        else
        { ;
        }
    }
    else
    {
        set_Tick_cnt = Get1MsTickVal();
    }


    if (high_side_st.cur_over30_flag == TRUE && 1 == Get1MsTickInterval(Tick_cnt_5s,TIME_5S) )
    {
//        if (PTC_MODE_OFF == high_side_st.ptc_mode_st && high_side_st.cur_over30_count <= 3u)
        if (high_side_st.cur_over30_count < 3u)
        {
            high_side_st.cur_over30_flag = FALSE;
            high_side_st.ptc_mode_st = PTC_MODE_INIT;

//            SHORT_PIN_Low();
        }
        else
        { ;
        }
    }
    else
    { ;
    }
}
/*
 * 函数介绍: 高压 过压检测
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void high_hv_over_hand(void)
{
    static uint32_t HV_over_Tick_cnt = 0u;
    static uint32_t HV_over_cancel_Tick_cnt = 0u;

    if (high_side_st.HV >= HV_OVER_VALUE && high_side_st.HV_voltage_st != VOLTAGE_OVER)
    {
        if ( 1 == Get1MsTickInterval(HV_over_Tick_cnt,TIME_1S) )
        {
            high_side_st.HV_voltage_st = VOLTAGE_OVER;
        }
        else
        { ;
        }
    }
    else
    {
        HV_over_Tick_cnt = Get1MsTickVal();
    }

    if (VOLTAGE_OVER == high_side_st.HV_voltage_st)
    {
        if (high_side_st.HV <= HV_OVER_CANCEL_VALUE)
        {
            if ( 1 == Get1MsTickInterval(HV_over_cancel_Tick_cnt,TIME_1S))
            {
                high_side_st.HV_voltage_st = VOLTAGE_VALID;
            }
            else
            { ;
            }
        }
        else
        {
            HV_over_cancel_Tick_cnt = Get1MsTickVal();
        }
    }
    else
    {
        HV_over_cancel_Tick_cnt = Get1MsTickVal();
    }
}
/*
 * 函数介绍: 高压 嵌压检测
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void high_hv_inlay_hand(void)
{
    static uint32_t set_Tick_cnt = 0u;
    static uint32_t cancel_Tick_cnt = 0u;

    if (high_side_st.HV <= HV_INVALID_VALUE && high_side_st.HV_voltage_st != VOLTAGE_INVALID)
    {
        high_side_st.HV_voltage_st = VOLTAGE_INVALID;
    }
    else if (high_side_st.HV_voltage_st == VOLTAGE_INVALID &&
             high_side_st.HV > HV_INVALID_VALUE)
    {
        high_side_st.HV_voltage_st = VOLTAGE_VALID;
    }
    else
    { ;
    }

    if (high_side_st.HV <= HV_INLAY_VALUE && high_side_st.HV_voltage_st != VOLTAGE_INLAY &&
        high_side_st.HV_voltage_st != VOLTAGE_INVALID)
    {
        if (1 == Get1MsTickInterval(set_Tick_cnt,TIME_1S))
        {
            high_side_st.HV_voltage_st = VOLTAGE_INLAY;
        }
        else
        { ;
        }
    }
    else
    {
        set_Tick_cnt = Get1MsTickVal();
    }

    if (VOLTAGE_INLAY == high_side_st.HV_voltage_st)
    {
        if (high_side_st.HV >= HV_INLAY_CANCEL_VALUE)
        {
            if (1 == Get1MsTickInterval(cancel_Tick_cnt,TIME_1S))
            {
                high_side_st.HV_voltage_st = VOLTAGE_VALID;
            }
            else
            { ;
            }
        }
        else
        {
            cancel_Tick_cnt = Get1MsTickVal();
        }
    }
    else
    {
        cancel_Tick_cnt = Get1MsTickVal();
    }
}
/*
 * 函数介绍: 检测15V的电压
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void high_v15_hand(void)
{
    static uint32_t set_Tick_cnt = 0u;
    static uint32_t cancel_Tick_cnt = 0u;

    if (high_ADC_OrgiData.V15.avg <= VBAT_3V_VALUE && (FALSE == high_side_st.v15_inlay))
    {
        if ( 1 == Get1MsTickInterval(set_Tick_cnt,TIME_1S))
        {
            high_side_st.v15_inlay = TRUE;
        }
        else
        { ;
        }
    }
    else
    {
        set_Tick_cnt = Get1MsTickVal();
    }

    if (TRUE == high_side_st.v15_inlay)
    {
        if (high_ADC_OrgiData.V15.avg >= VBAT_3_5V_VALUE)
        {
            if ( 1 == Get1MsTickInterval(cancel_Tick_cnt,TIME_1S))
            {
                high_side_st.v15_inlay = FALSE;
            }
            else
            { ;
            }
        }
        else
        {
            cancel_Tick_cnt = Get1MsTickVal();
        }
    }
    else
    {
        cancel_Tick_cnt = Get1MsTickVal();
    }
}
/*
 * 函数介绍: IGBT短路检测
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void high_IGBT_short(void)
{
    static uint32_t set_Tick_cnt = 0u;
    uint16_t current = high_ADC_OrgiData.AMP.avg;

    if (PTC_MODE_OFF == high_side_st.ptc_mode_st && current >= CURRENT_3A &&
        (IGBT_ST_SHORT != high_side_st.IGBT_status))
    {
        if ( 1 == Get1MsTickInterval(set_Tick_cnt,TIME_0_5S) )
        {
            high_side_st.IGBT_status = IGBT_ST_SHORT;
            high_side_st.ptc_mode_st = PTC_MODE_OFF;
            pwm_off_st = PWM_OFF_IGBT_SHORT;
        }
        else
        { ;
        }
    }
    else
    {
        set_Tick_cnt = Get1MsTickVal();
    }
}

/*
 * 函数介绍: IGBT开路检测
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void high_IGBT_open(void)
{
    static uint32_t set_Tick_cnt = 0u;
    float current = high_side_st.current;
    igbt_open_test =0;

        if (current < CURRENT_0_5A )
        {
            igbt_open_test |= 0x01;
        }
        if (high_side_st.ptc_mode_st != PTC_MODE_OFF )
        {
            igbt_open_test |= 0x02;
        }

        if (high_side_st.HV > 100u)
        {
            igbt_open_test |= 0x04;
        }

        if (high_side_st.HV > 100u && current < CURRENT_0_5A && high_side_st.ptc_mode_st != PTC_MODE_OFF)
        {
            igbt_open_test |= 0x10;
            if (1 == Get1MsTickInterval(set_Tick_cnt,TIME_5S))
            {
                igbt_open_test |= 0x20;
                high_side_st.IGBT_status = IGBT_ST_OPEN;
            }
            else
            { ;
            }
        }
        else
        {
            igbt_open_test |= 0x80;
            set_Tick_cnt = Get1MsTickVal();
        }
}

/*
 * 函数介绍: NTC开路检测
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void high_ntc_open(void)
{
    static uint32_t set_Tick_cnt = 0u;
    static uint32_t cancel_Tick_cnt = 0u;
    uint8_t temp = (low_ADC_OrgiData.sensor1_out.max >= NTC_OPEN) ||
                   (low_ADC_OrgiData.sensor2_in.max >= NTC_OPEN);

    if (temp && ADC_VALUE_OPEN != high_side_st.ntc_status)
    {
        if ( 1 == Get1MsTickInterval(set_Tick_cnt,TIME_1S))
        {
            high_side_st.ntc_status = ADC_VALUE_OPEN;
        }
        else
        { ;
        }
    }
    else
    {
        set_Tick_cnt = Get1MsTickVal();
    }

    if (ADC_VALUE_OPEN == high_side_st.ntc_status)
    {
        if (temp == FALSE)
        {
            if ( 1 == Get1MsTickInterval(cancel_Tick_cnt,TIME_1S))
            {
                high_side_st.ntc_status = ADC_VALUE_VALID;
            }
            else
            { ;
            }
        }
        else
        {
            cancel_Tick_cnt = Get1MsTickVal();
        }
    }
    else
    {
        cancel_Tick_cnt = Get1MsTickVal();
    }
}

/*
 * 函数介绍: NTC短路检测
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void high_ntc_short(void)
{
    static uint32_t set_Tick_cnt = 0u;
    static uint32_t cancel_Tick_cnt = 0u;
    uint8_t temp = (low_ADC_OrgiData.sensor1_out.min <= NTC_SHORT) ||
                   (low_ADC_OrgiData.sensor2_in.min <= NTC_SHORT);

    if (temp && ADC_VALUE_SHORT != high_side_st.ntc_status)
    {
        if (1 == Get1MsTickInterval(set_Tick_cnt,TIME_1S))
        {
            high_side_st.ntc_status = ADC_VALUE_SHORT;
        }
        else
        { ;
        }
    }
    else
    {
        set_Tick_cnt = Get1MsTickVal();
    }

    if (ADC_VALUE_SHORT == high_side_st.ntc_status)
    {
        if (temp == FALSE)
        {
            if (1 == Get1MsTickInterval(cancel_Tick_cnt,TIME_1S))
            {
                high_side_st.ntc_status = ADC_VALUE_VALID;
            }
            else
            { ;
            }
        }
        else
        {
            cancel_Tick_cnt = Get1MsTickVal();
        }
    }
    else
    {
        cancel_Tick_cnt = Get1MsTickVal();
    }
}

/*
 * 函数介绍: 错误检测
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void high_error_check(void)
{
    high_cur60A_hand();
    high_cur30A_hand();
    high_hv_over_hand();
    high_hv_inlay_hand();
#ifndef PWM_TEST
    high_v15_hand();
    high_IGBT_short();
    high_IGBT_open();
    /*chnell err*/
//    high_ntc_open();
//    high_ntc_short();
#endif
}
void high_ptc_error_close(void)
{
   if (PTC_MODE_OFF != high_side_st.ptc_mode_st)
   {
       if (PTC_ERROR_NONE != high_uart_tx_st.sig.ptc_all_error_status &&
           PTC_ERROR_PCB_OVER_120 != high_uart_tx_st.sig.ptc_all_error_status)
       {
           high_side_st.ptc_mode_st = PTC_MODE_OFF;
       }
   }
   else
   {
       ;
   }
}

void high_error_back(void)
{
    if (high_uart_rx_st.sig.ptc_error_status > PTC_ERROR_NONE)
    {
        high_uart_tx_st.sig.ptc_all_error_status = high_uart_rx_st.sig.ptc_error_status;
    }
    else
    {
        if (TRUE == high_side_st.cur_over60_flag)
        {
            high_uart_tx_st.sig.ptc_all_error_status = PTC_HIGH_CUR60A;
        }
        else if (VOLTAGE_OVER == high_side_st.HV_voltage_st)
        {
            high_uart_tx_st.sig.ptc_all_error_status = PTC_HIGH_HV_OVER;
        }
        else if (VOLTAGE_INLAY == high_side_st.HV_voltage_st)
        {
            high_uart_tx_st.sig.ptc_all_error_status = PTC_HIGH_HV_INLAY;
        }
        else if (TRUE == high_side_st.cur_over30_flag)
        {
            high_uart_tx_st.sig.ptc_all_error_status = PTC_HIGH_CUR30A;
        }
        else if (TRUE == high_side_st.v15_inlay)
        {
            high_uart_tx_st.sig.ptc_all_error_status = PTC_HIGH_V15_LOW;
        }
        else if (IGBT_ST_SHORT == high_side_st.IGBT_status)
        {
            high_uart_tx_st.sig.ptc_all_error_status = PTC_HIGH_IGBT_SHORT;
        }
        else if (IGBT_ST_OPEN == high_side_st.IGBT_status)
        {
            high_uart_tx_st.sig.ptc_all_error_status = PTC_HIGH_IGBT_OPEN;
        }
        else if (ADC_VALUE_OPEN == high_side_st.ntc_status)
        {
            high_uart_tx_st.sig.ptc_all_error_status = PTC_HIGH_WATER_NTC_OPEN;
        }
        else if (ADC_VALUE_SHORT == high_side_st.ntc_status)
        {
            high_uart_tx_st.sig.ptc_all_error_status = PTC_HIGH_WATER_NTC_SHORT;
        }
#if 0
        else if (PTC_MODE_INVALID  == high_uart_rx_st.sig.mode)
        {
            high_uart_tx_st.sig.ptc_all_error_status = PTC_PTC_MODE_INVALID;
        }
#endif
        else if (HVH_MODE_OFF == high_uart_rx_st.sig.ptc_off_on)
        {
            high_uart_tx_st.sig.ptc_all_error_status = PTC_HVH_OFF;
        }
        else
        {
            high_uart_tx_st.sig.ptc_all_error_status = PTC_ERROR_NONE;
        }
    }
}
#if 1
/*
 * 函数介绍: PID调试功率
 * 参数:
 * 返回值: {*}
 * 备注:
 */
static void high_pid_power_test(uint16_t set_power)
{
    static uint32_t tick = 0;
    if (set_power != 0u)
    {
        if (PWM_OFF_POWER0 == pwm_off_st)
        {
            pwm_off_st = PWM_OFF_NOT;
        }
        else
        { ;
        }

        if ( 1 == Get1MsTickInterval(tick,100) )
        {
            uint16_t cur_power = 0;
            cur_power = high_side_st.current * high_side_st.HV;

            high_side_st.act_power = cur_power;

//        PID_power_st.Ref = set_power;
            PID_power_st.Fdb = cur_power;

            pid_func(&PID_power_st);
            PWM_IGBT_duty(PID_power_st.Out);
            high_side_st.set_pwm = PID_power_st.Out;

            tick = Get1MsTickVal();
        }
    }
    else
    {
        tick = Get1MsTickVal();
        pwm_off_st = PWM_OFF_POWER0;
    }
}
#endif
/*
 * 函数介绍: 高边处理函数
 * 参数:
 * 返回值: {*}
 * 备注:
 */
void high_handle(void)
{
    high_out_temp();
    if (TRUE == adc_get_curr_status())
    {
        if (high_side_st.current_init == 0u) //存在问题
        {
            high_side_st.current_init = adc_get_curr_init();
        }
        else
        { ;
        }

        high_ptc_mode();
        high_ADC_convert();
        high_send_data_handle();
        high_uart30_rx_tx();
        //high_uart31_rx_tx();无该功能接口
        high_IGBT_status();

        high_error_check();
        high_ptc_error_close();
        high_error_back();
#ifndef PWM_TEST
                //high_pid_power_test(PID_power_st.Ref);

#endif
    }else
    {
        ;
    }
}
